﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.Infrastructure;

namespace NTQOnlineExam.Services.DBCommand.Account
{
    public class DeleteUserAccountDbCommand:ICommand<bool>
    {
        private readonly int _userId;
        private const string SQL = @"
                DELETE FROM dbo.UserSession where userId= @UserId;
                DELETE FROM dbo.webpages_Membership where UserId = @UserId;
                DELETE FROM dbo.webpages_UsersInRoles where UserId = @UserId;
                UPDATE dbo.UserProfile SET IsDeleted = 1 where UserId = @UserId;
            ";
        public DeleteUserAccountDbCommand(int userId)
        {
            _userId = userId;
        }

        public bool Execute()
        {
            var connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

            using (var con = new SqlConnection(connectionString))
            {
                using (var cmd = new SqlCommand(SQL, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = _userId;
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            return true;

        }
    }
}

